<template>
  <smart-page id="system-menu">
    <template #actions>
      <a-button @click="treePage.toggleTree()" v-text="toggleText"/>
    </template>
    <a-table
        v-model:expandedKeys="expandedKeys"
        :table-layout-fixed="theme.layoutFixed"
        :columns="ORGAN_COLUMNS"
        :row-key="rowKey"
        :column-resizable="theme.resizable"
        :bordered="theme.bordered"
        :stripe="theme.stripe"
        :sticky-header="theme.sticky"
        :hoverable="theme.hoverAble"
        :loading="theme.loading"
        :data="dataSource">

      <template #action="{ record }">
        <a-space size="mini">
          <a-link  v-auth="Acl.SYSTEM_ORG_ORGAN_ADD"   v-if="record.quality.value==='HEADQUARTERS'" @click="editCtr.open({ parentCode:record.code,quality:'BRANCH'})" v-text="'建分部'"/>
          <a-link v-auth="Acl.SYSTEM_ORG_ORGAN_UPDATE" @click="editCtr.open(record)" v-text="'修改'"/>
          <a-link v-auth="Acl.SYSTEM_ORG_ORGAN_DELETE"  v-if="record.parentCode !=='0'" status="danger" @click="onDelete(record)"> 删除</a-link>
        </a-space>
      </template>
    </a-table>
    <EditModal v-if="editCtr.show" :record="editCtr.record" @cancel="editCtr.close"/>
  </smart-page>


</template>
<script lang="ts" setup  name="system-organ-main">
import {onMounted} from 'vue';
import {TreePage} from "@/libs/core";
import {ORGAN_COLUMNS} from "../resource/colums";
import {SystemHttp} from "../resource/http";
import EditModal from './edit.vue'
import {Acl} from "@/libs/acl";

const {getOrganList, delOrgan} = SystemHttp
const pageConfig = {title: '机构', rowKey: 'id', nameKey: 'name'}
const treePage = new TreePage(pageConfig).setFetchXhr(getOrganList).setDeleteXhr(delOrgan).setTreeMap("0", "code", "parentCode")
    .setPermission(Acl.SYSTEM_ORG_ORGAN_LIST)
const {theme, rowKey, dataSource, editCtr, expandedKeys,} = treePage
const {onFetch, onDelete, toggleText} = treePage
onMounted(async () => {
  await onFetch()
  treePage.toggleTree()
})
</script>

<style lang="less" scoped>
:deep( .smart-page-header) {
  margin-bottom: 1px !important;
}
</style>